library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.2
# Wave 1
# Binary for confidence
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

# Mil is going Repub, overall
mil_conf.df$DOV_REP <- NA
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_REP[mil_conf.df$DOV_ASSIGNMENT_A == 2] <- 1
summary(factor(mil_conf.df$DOV_REP))

# MIl is going Dem, overall
mil_conf.df$DOV_DEM <- NA
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_DEM[mil_conf.df$DOV_ASSIGNMENT_A == 3] <- 1
summary(factor(mil_conf.df$DOV_DEM))

# Mil is partisan, overall
mil_conf.df$DOV_PART <- NA
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_PART[mil_conf.df$DOV_ASSIGNMENT_A == 4] <- 1
summary(factor(mil_conf.df$DOV_PART))


# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

# Mil is Repub overall
summary(factor(df$P_ASSIGN1))
df$REPUB <- NA
df$REPUB[df$P_ASSIGN1 == 1] <- 0
df$REPUB[df$P_ASSIGN1 == 6] <- 1
summary(factor(df$REPUB))

# Mil is Dem overall
df$DEMOC <- NA
df$DEMOC[df$P_ASSIGN1 == 1] <- 0
df$DEMOC[df$P_ASSIGN1 == 7] <- 1
summary(factor(df$DEMOC))

# Mil is partisan overall
df$PARTISAN <- NA
df$PARTISAN[df$P_ASSIGN1 == 1] <- 0
df$PARTISAN[df$P_ASSIGN1 == 8] <- 1
summary(factor(df$PARTISAN))

# Drop respondents without party ID
summary(factor(df$party))
df2 <- df[!is.na(df$party),]


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

# Wave 1
# dov_assignment_a == 2 - REPUBLICAN OFFICERS
w1r_d <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==0])
w1r_i <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==1])
w1r_r <- svyttest(Q8a~DOV_REP, w1_design[w1_design$variables$party==2])

# dov 3 - DEMOCRAT OFFICERS
w1d_d <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==0])
w1d_i <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==1])
w1d_r <- svyttest(Q8a~DOV_DEM, w1_design[w1_design$variables$party==2])

# dov 4 - Partisan military OFFICERS
w1p_d <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==0])
w1p_i <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==1])
w1p_r <- svyttest(Q8a~DOV_PART, w1_design[w1_design$variables$party==2])


# Wave 2
# P_ASSIGN1 == 6 REPUBLICAN OFFICERS
w2r_d <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==0])
w2r_i <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==1])
w2r_r <- svyttest(Q11_d~REPUB, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 7 DEMOCRAT OFFICERS
w2d_d <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==0])
w2d_i <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==1])
w2d_r <- svyttest(Q11_d~DEMOC, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 8 Partisan military OFFICERS
w2p_d <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==0])
w2p_i <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==1])
w2p_r <- svyttest(Q11_d~PARTISAN, w2_design[w2_design$variables$party==2])


# Wave 1
pt.est1 <- c(w1r_d$estimate,w1r_i$estimate,w1r_r$estimate,
             w1d_d$estimate,w1d_i$estimate,w1d_r$estimate,
             w1p_d$estimate,w1p_i$estimate,w1p_r$estimate)
ci.low1 <- c(w1r_d$conf.int[1],w1r_i$conf.int[1],w1r_r$conf.int[1],
             w1d_d$conf.int[1],w1d_i$conf.int[1],w1d_r$conf.int[1],
             w1p_d$conf.int[1],w1p_i$conf.int[1],w1p_r$conf.int[1])
ci.high1 <- c(w1r_d$conf.int[2],w1r_i$conf.int[2],w1r_r$conf.int[2],
              w1d_d$conf.int[2],w1d_i$conf.int[2],w1d_r$conf.int[2],
              w1p_d$conf.int[2],w1p_i$conf.int[2],w1p_r$conf.int[2])
plot.labs <- c(" ","Republican\nMilitary","  ",
               "   ","Democrat\nMilitary","    ",
               "     ","Partisan\nMilitary", "      ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                      lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.4,.4)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 1")+
  theme_bw()

# Wave 2
pt.est2 <- c(w2r_d$estimate,w2r_i$estimate,w2r_r$estimate,
             w2d_d$estimate,w2d_i$estimate,w2d_r$estimate,
             w2p_d$estimate,w2p_i$estimate,w2p_r$estimate)
ci.low2 <- c(w2r_d$conf.int[1],w2r_i$conf.int[1],w2r_r$conf.int[1],
             w2d_d$conf.int[1],w2d_i$conf.int[1],w2d_r$conf.int[1],
             w2p_d$conf.int[1],w2p_i$conf.int[1],w2p_r$conf.int[1])
ci.high2 <- c(w2r_d$conf.int[2],w2r_i$conf.int[2],w2r_r$conf.int[2],
              w2d_d$conf.int[2],w2d_i$conf.int[2],w2d_r$conf.int[2],
              w2p_d$conf.int[2],w2p_i$conf.int[2],w2p_r$conf.int[2])
plot.labs <- c(" ","Republican\nMilitary","  ",
               "   ","Democrat\nMilitary","    ",
               "     ","Partisan\nMilitary", "      ")
df.plot2 <- data.frame(Treatment=plot.labs, estimate=pt.est2, 
                       lower=ci.low2, upper=ci.high2)
df.plot2$Treatment <- factor(df.plot2$Treatment, levels = plot.labs)

plot_w2 <- ggplot()+
  geom_pointrange(data=df.plot2, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.4,.4)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  ggtitle(label="Wave 2")+
  theme_bw()

grid.arrange(plot_w1, plot_w2, nrow=1, ncol=2)
